<?php
namespace app\api\controller;

use app\common\controller\Api;
//个人中心接口
class Center extends Api
{
    protected $noNeedLogin = ['upload','uploadMulti','agreement','test'];
    protected $noNeedRight = '*';
    protected $websiteUrl = 'http://api.yjzufang.com';









    //获取个人信息
    public function userInfo(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $info = db("user")->field("avatar,mobile,nickname")->where("id",$userInfo['id'])->find();
        if(!$info['avatar']){
            $info['avatar'] = '/assets/img/portrait.png';
        }
        $this->success("返回成功",$info);
    }


        //单图上传(销售端和用户端共用接口)
     public  function upload()
     {
            header('Access-Control-Allow-Origin: *');
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
            ob_clean();
            $file = $this->request->file("file");
            $img = "";
            if(!$file){
                $this->error('请上传文件');
            }
            if ($file){
                $info = $file->move('./uploads/');
                if ($info) {
                    $getSaveName = str_replace("\\", "/", $info->getSaveName());
                    $img_name = $getSaveName;
                    $img = "/uploads/".$img_name;
                    $this->success('上传成功',$img);
                } else {
                    $this->error('上传错误'.$file->getError());
                }
            }
     }

    //多图上传
    public function uploadMulti(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $files = $this->request->file("file");
        //$this->success('fff',$files);
       // die;
        if($files) {
            $path_arr = [];
            foreach ($files as $key => $file) {
                $infos = $file->move('./uploads/');
                if ($infos) {
                    $img_name = $infos->getSavename();
                    $filename = "/uploads/" . $img_name;
                    $path_arr = $filename;
                }
            }
            $path = implode(",", $path_arr); //拼接后的顶部图片集或视频集
            $this->success('返回成功',$path);
        }else{
            $this->error("请选择文件");
        }


    }

        //修改头像(销售端和用户端共用接口)
        public function avatarEdit(){
            header('Access-Control-Allow-Origin: *');
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
            ob_clean();
            $userInfo = $this->auth->getUserinfo();
            $avatar = $this->request->request("avatar");

            $res = db('user')->where("id",$userInfo['id'])->update(['avatar'=>$avatar]);
            if($res){
                $this->success("修改成功",$avatar);
            }else{
                $this->error("修改失败");
            }
        }




    //房源浏览记录
    public function  houseView(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $userInfo = $this->auth->getUserinfo();
        $page = $this->request->request("page");
        $list = db("yjy_view a")
            ->join("yjy_house b","a.house_id = b.id","LEFT")
            ->join("user c","a.user_id = c.id","LEFT")
            ->join("yjy_house_layout d","d.id = b.layout_id","LEFT")
            ->field("b.id,b.image,b.images,b.title,b.location_near,b.price,d.name,c.nickname,c.mobile,c.avatar")
            ->field("from_unixtime(a.createtime,'%m/%d %H:%i') as createtime")
            ->where("a.agent_id",$userInfo['id'])
            ->paginate(6);

        $this->success("返回列表",$list);
    }

    //看房记录
    public function houseVisit(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $page = $this->request->request("page");
        $list_visit = db('yjy_house_visit_log a')
            ->join("yjy_house b","a.house_id = b.id","LEFT")
            ->join("yjy_house_layout c","c.id = b.layout_id","LEFT")
            ->field("a.house_id,b.title,b.image,b.images,b.location_near,b.price,b.status,c.name as layout,a.mobile")
            ->field("a.renter_name,from_unixtime(a.createtime,'%m/%d %H:%i') as createtime")
            ->where('a.user_id',$userInfo['id'])
            ->paginate(6);
        $this->success("返回成功",$list_visit);
    }


    //我的成交
    public function mydeal(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $page = $this->request->request("page");
        $list = db("yjy_deal a")
            ->join("yjy_house b","b.id = a.house_id","LEFT")
            ->join("yjy_house_layout c","c.id = b.layout_id","LEFT")
            ->field("a.id,a.house_id,b.title,b.image,b.images,b.location_near,b.price,b.status,c.name as layout")
            ->field("a.renter_name,from_unixtime(a.intime,'%Y/%m/%d') as intime,a.agreement_images")
            ->where("a.agent_id",$userInfo['id'])
            ->paginate(6);

        $this->success("返回成功",$list);

    }


    //查看合同
    public function  agreement(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $agreement_id = $this->request->request("agreement_id");
        $image = db("yjy_deal")->where("id",$agreement_id)->value("agreement_images");
        $arr = explode(",",$image);
        foreach($arr as $key=>$val){
            $arr[$key] = $this->websiteUrl.$val;
        }
        $this->success("返回成功",$arr);

    }







}